package com.kangyi.admin.controller;

import com.kangyi.admin.service.GuestsStayingService;
import com.kangyi.dto.GuestsStayingDTO;
import com.kangyi.dto.GuestsStayingPageQueryDTO;
import com.kangyi.dto.UpdateGuestsStayingDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.GuestsStayingDetailVO;
import com.kangyi.vo.GuestsStayingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController("adminGuestsStayingController")
@RequestMapping("/admin/guests")
@Slf4j
@Api(tags = "养老院-人员档案相关接口")
public class GuestsStayingController {

    @Autowired
    private GuestsStayingService guestsStayingService;

    @GetMapping("/page")
    @ApiOperation("人员档案分页接口")
    public Result<PageResult> pageQuery(GuestsStayingPageQueryDTO guestsStayingPageQueryDTO){
        log.info("人员档案分页接口:{}",guestsStayingPageQueryDTO);
        try {
            PageResult pageResult = guestsStayingService.pageQuery(guestsStayingPageQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除人员档案")
    @CacheEvict(cacheNames = "guestsStaying_", allEntries = true)
    public Result deleteGuests(@RequestParam List<Integer> ids){
        log.info("批量删除人员档案：{}",ids);
        try {
            guestsStayingService.deleteGuests(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("查询入住人员详情")
//    @Cacheable(cacheNames = "guestsStaying_", key = "#id")
    public Result<GuestsStayingDetailVO> detailGuest(@PathVariable Integer id){
        log.info("查询入住人员详情");
        try {
            GuestsStayingDetailVO guestsStayingDetailVO = guestsStayingService.detailGuest(id);
            return Result.success(guestsStayingDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @ApiOperation("人员档案编辑")
    @CacheEvict(cacheNames = "guestsStaying_", allEntries = true)
    public Result editGuest(@RequestBody GuestsStayingDTO guestsStayingDTO){
        log.info("人员档案编辑:{}",guestsStayingDTO);
        try {
            guestsStayingService.editGuest(guestsStayingDTO);
            return Result.success("编辑成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


}
